package QuizUI;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import QuizPackage.Quiz;
import QuizPackage.QuizDBC;

/**
 * Servlet implementation class EndQuiz
 */
@WebServlet("/EndQuiz")
public class EndQuiz extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EndQuiz() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatch = request.getRequestDispatcher("EndQuiz.jsp");
		dispatch.forward(request,response);
		
		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		Quiz quiz = (Quiz) session.getAttribute(request.getParameter("quizid"));
		int quizid = Integer.parseInt(request.getParameter("quizid"));//??
		
		quiz.endQuiz();
		
		int score = quiz.getScore();
		int total = quiz.getTotal();
		int time = quiz.getTime();
		
		out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\""
				      + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
		out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
		out.println("<head>");
		
		out.println("<title>"+quiz.getName()+"</title>");
		out.println("</head>");
		
		out.println("<body>");

		out.println("<h1>You have finished "+quiz.getName()+"!</h1>");
		if(quiz.isPractice()) out.println("(In practice mode)");
		
		out.print("<p>You received a "+score+"/"+total+".  ");
		if(((double)score)/total>.9) out.println("Great job!!!</p>");
		else if(((double)score)/total>.8) out.println("Good job!!</p>");
		else if(((double)score)/total>.7) out.println("Solid score!</p>");
		else if(((double)score)/total>.6) out.println("Sorry :(</p>");
		
		out.println("You took "+time/60+" minutes and "+time%60+" seconds to complete the quiz.</p>");
		
		QuizDBC dbc = new QuizDBC();
		Date d = new Date();
		if(!quiz.isPractice()) dbc.enterResult(quizid,(String)session.getAttribute("username"),score,total,time,d.getTime());
		//dbc.enterResult(quizid,USERID,score,total,time,d.getTime());
		
		out.println("<form action=\"StartQuiz\" method=\"get\">");
		out.println("<input name=\"quizid\" type =\"hidden\" value=\""+quizid+"\"/>");
		out.println("<p>Click to take the quiz again!</p>");
		out.println("<input type=\"submit\" value=\"Again\"/>");
		out.println("</form>");
		
		out.println("</body>");
		out.println("</html>");*/
	}

}
